package org.jivesoftware.smackx.pubsub;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.jivesoftware.smackx.pubsub.packet.SyncPacketSend;

public class LeafNode extends Node
{
  public LeafNode(Connection paramConnection, String paramString)
  {
    super(paramConnection, paramString);
  }

  public void deleteAllItems()
    throws XMPPException
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.SET, new NodeExtension(PubSubElementType.PURGE_OWNER, getId()), PubSubElementType.PURGE_OWNER.getNamespace());
    SyncPacketSend.getReply(this.con, localPubSub);
  }

  public void deleteItem(String paramString)
    throws XMPPException
  {
    ArrayList localArrayList = new ArrayList(1);
    localArrayList.add(paramString);
    deleteItem(localArrayList);
  }

  public void deleteItem(Collection<String> paramCollection)
    throws XMPPException
  {
    ArrayList localArrayList = new ArrayList(paramCollection.size());
    Iterator localIterator = paramCollection.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        PubSub localPubSub = createPubsubPacket(IQ.Type.SET, new ItemsExtension(ItemsExtension.ItemsElementType.retract, getId(), localArrayList));
        SyncPacketSend.getReply(this.con, localPubSub);
        return;
      }
      localArrayList.add(new Item((String)localIterator.next()));
    }
  }

  public DiscoverItems discoverItems()
    throws XMPPException
  {
    DiscoverItems localDiscoverItems = new DiscoverItems();
    localDiscoverItems.setTo(this.to);
    localDiscoverItems.setNode(getId());
    return ((DiscoverItems)SyncPacketSend.getReply(this.con, localDiscoverItems));
  }

  public <T extends Item> List<T> getItems()
    throws XMPPException
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.GET, new GetItemsRequest(getId()));
    return ((ItemsExtension)((PubSub)SyncPacketSend.getReply(this.con, localPubSub)).getExtension(PubSubElementType.ITEMS)).getItems();
  }

  public <T extends Item> List<T> getItems(int paramInt)
    throws XMPPException
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.GET, new GetItemsRequest(getId(), paramInt));
    return ((ItemsExtension)((PubSub)SyncPacketSend.getReply(this.con, localPubSub)).getExtension(PubSubElementType.ITEMS)).getItems();
  }

  public <T extends Item> List<T> getItems(int paramInt, String paramString)
    throws XMPPException
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.GET, new GetItemsRequest(getId(), paramString, paramInt));
    return ((ItemsExtension)((PubSub)SyncPacketSend.getReply(this.con, localPubSub)).getExtension(PubSubElementType.ITEMS)).getItems();
  }

  public <T extends Item> List<T> getItems(String paramString)
    throws XMPPException
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.GET, new GetItemsRequest(getId(), paramString));
    return ((ItemsExtension)((PubSub)SyncPacketSend.getReply(this.con, localPubSub)).getExtension(PubSubElementType.ITEMS)).getItems();
  }

  public <T extends Item> List<T> getItems(Collection<String> paramCollection)
    throws XMPPException
  {
    ArrayList localArrayList = new ArrayList(paramCollection.size());
    Iterator localIterator = paramCollection.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        PubSub localPubSub = createPubsubPacket(IQ.Type.GET, new ItemsExtension(ItemsExtension.ItemsElementType.items, getId(), localArrayList));
        return ((ItemsExtension)((PubSub)SyncPacketSend.getReply(this.con, localPubSub)).getExtension(PubSubElementType.ITEMS)).getItems();
      }
      localArrayList.add(new Item((String)localIterator.next()));
    }
  }

  public void publish()
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.SET, new NodeExtension(PubSubElementType.PUBLISH, getId()));
    this.con.sendPacket(localPubSub);
  }

  public <T extends Item> void publish(Collection<T> paramCollection)
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.SET, new PublishItem(getId(), paramCollection));
    this.con.sendPacket(localPubSub);
  }

  public <T extends Item> void publish(T paramT)
  {
    Object localObject;
    ArrayList localArrayList = new ArrayList(1);
    if (paramT == null)
      localObject = new Item();
    while (true)
    {
      localArrayList.add(localObject);
      publish(localArrayList);
      return;
      localObject = paramT;
    }
  }

  public void send()
    throws XMPPException
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.SET, new NodeExtension(PubSubElementType.PUBLISH, getId()));
    SyncPacketSend.getReply(this.con, localPubSub);
  }

  public <T extends Item> void send(Collection<T> paramCollection)
    throws XMPPException
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.SET, new PublishItem(getId(), paramCollection));
    SyncPacketSend.getReply(this.con, localPubSub);
  }

  public <T extends Item> void send(T paramT)
    throws XMPPException
  {
    Object localObject;
    ArrayList localArrayList = new ArrayList(1);
    if (paramT == null)
      localObject = new Item();
    while (true)
    {
      localArrayList.add(localObject);
      send(localArrayList);
      return;
      localObject = paramT;
    }
  }
}